'''
Author: xialang333@gmail.com
Date: 2025-08-04 14:34:01
LastEditTime: 2025-08-04 16:40:24
'''
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List

from app import crud, schemas
from app.database import get_db

router = APIRouter()

@router.post("/exporters/", response_model=schemas.Exporter)
def create_exporter(exporter: schemas.ExporterCreate, db: Session = Depends(get_db)):
    db_exporter = crud.get_exporter_by_name(db, name=exporter.name)
    if db_exporter:
        raise HTTPException(status_code=400, detail="Exporter already registered")
    return crud.create_exporter(db=db, exporter=exporter)

@router.get("/exporters/", response_model=List[schemas.Exporter])
def read_exporters(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    exporters = crud.get_exporters(db, skip=skip, limit=limit)
    return exporters

@router.get("/exporters/{exporter_id}", response_model=schemas.Exporter)
def read_exporter(exporter_id: int, db: Session = Depends(get_db)):
    db_exporter = crud.get_exporter(db, exporter_id=exporter_id)
    if db_exporter is None:
        raise HTTPException(status_code=404, detail="Exporter not found")
    return db_exporter

@router.put("/exporters/{exporter_id}", response_model=schemas.Exporter)
def update_exporter(exporter_id: int, exporter: schemas.ExporterCreate, db: Session = Depends(get_db)):
    db_exporter = crud.update_exporter(db, exporter_id=exporter_id, exporter=exporter)
    if db_exporter is None:
        raise HTTPException(status_code=404, detail="Exporter not found")
    return db_exporter

@router.delete("/exporters/{exporter_id}")
def delete_exporter(exporter_id: int, db: Session = Depends(get_db)):
    db_exporter = crud.delete_exporter(db, exporter_id=exporter_id)
    if db_exporter is None:
        raise HTTPException(status_code=404, detail="Exporter not found")
    return {"ok": True}